package com.ruoyi.web.controller.car;

import com.ruoyi.car.domain.CarOwner;
import com.ruoyi.car.service.ICarOwnerService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车主信息Controller
 * 
 * @author ruoyi
 * @date 2020-05-11
 */
@Controller
@RequestMapping("/car/owner")
public class CarOwnerController extends BaseController
{
    private String prefix = "car/owner";

    @Autowired
    private ICarOwnerService carOwnerService;

    @RequiresPermissions("car:owner:view")
    @GetMapping()
    public String owner()
    {
        return prefix + "/owner";
    }

    /**
     * 查询车主信息列表
     */
    @RequiresPermissions("car:owner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarOwner carOwner)
    {
        startPage();
        List<CarOwner> list = carOwnerService.selectCarOwnerList(carOwner);
        return getDataTable(list);
    }
    /**
     * 查看车主详情
     */
//    @RequiresPermissions("car:owner:detail")
//    @GetMapping("/detail/{ownerId}")
//    public String detail(@PathVariable("ownerId") Long ownerId, ModelMap mmap)
//    {
//        mmap.put("owner", carOwnerService.selectCarOwnerById(ownerId));
//        return prefix + "/detail";
//    }

    /**
     * 导出车主信息列表
     */
    @RequiresPermissions("car:owner:export")
    @Log(title = "车主信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarOwner carOwner)
    {
        List<CarOwner> list = carOwnerService.selectCarOwnerList(carOwner);
        ExcelUtil<CarOwner> util = new ExcelUtil<CarOwner>(CarOwner.class);
        return util.exportExcel(list, "owner");
    }

    /**
     * 新增车主信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车主信息
     */
    @RequiresPermissions("car:owner:add")
    @Log(title = "车主信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarOwner carOwner)
    {
        return toAjax(carOwnerService.insertCarOwner(carOwner));
    }

    /**
     * 修改车主信息
     */
    @GetMapping("/edit/{ownerId}")
    public String edit(@PathVariable("ownerId") Long ownerId, ModelMap mmap)
    {
        CarOwner carOwner = carOwnerService.selectCarOwnerById(ownerId);
        mmap.put("carOwner", carOwner);
        return prefix + "/edit";
    }




    /**
     * 修改保存车主信息
     */
    @RequiresPermissions("car:owner:edit")
    @Log(title = "车主信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarOwner carOwner)
    {
        return toAjax(carOwnerService.updateCarOwner(carOwner));
    }

    /**
     * 删除车主信息
     */
    @RequiresPermissions("car:owner:remove")
    @Log(title = "车主信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carOwnerService.deleteCarOwnerByIds(ids));
    }
}
